package com.quickboot.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.date.DateUtil;
import com.quickboot.common.annotation.Log;
import com.quickboot.common.consts.MsgConst;
import com.quickboot.common.controller.BaseController;
import com.quickboot.common.entity.Result;
import com.quickboot.common.entity.TableResult;
import com.quickboot.common.utils.EasyPoiExcelUtils;
import com.quickboot.system.entity.SysUser;
import com.quickboot.system.service.SysUserService;
import com.quickboot.system.vo.SysUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 用户
 *
 * @author Tellsea
 * @date 2022-11-27
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/system/sysUser")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class SysUserController extends BaseController {

    private final SysUserService sysUserService;

    @ApiOperation("查询列表")
    @Log("查询列表")
    @SaCheckPermission("system:sysUser:list")
    @GetMapping("/list")
    public TableResult<SysUserVo> queryList(SysUserVo entity) {
        return TableResult.ok(sysUserService.queryList(getPage(), entity));
    }

    @ApiOperation("导入")
    @Log("导入")
    @SaCheckPermission("system:sysUser:import")
    @PostMapping("/importExcel")
    public Result importExcel(@RequestParam("file") MultipartFile file) {
        List<SysUser> list = EasyPoiExcelUtils.importExcel(file, 1, 1, SysUser.class);
        sysUserService.saveBatch(list, list.size());
        return Result.ok(MsgConst.IMPORT_SUCCESS + list.size() + "条数据");
    }

    @ApiOperation("导出")
    @Log("导出")
    @SaCheckPermission("system:sysUser:export")
    @GetMapping("/exportExcel")
    public void exportExcel(SysUserVo entity) {
        List<SysUserVo> list = sysUserService.queryList(getAllPage(), entity).getRecords();
        EasyPoiExcelUtils.exportExcel(list, "用户-" + DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss"), SysUserVo.class);
    }

    @ApiOperation("根据ID查询")
    @Log("根据ID查询")
    @SaCheckPermission("system:sysUser:get")
    @GetMapping("/queryById/{id}")
    public Result<SysUserVo> queryById(@PathVariable("id") Long id) {
        return queryResult(sysUserService.queryById(id));
    }

    @ApiOperation("新增")
    @Log("新增")
    @SaCheckPermission("system:sysUser:save")
    @PostMapping("/save")
    public Result save(@RequestBody SysUserVo entity) {
        return sysUserService.createUser(entity);
    }

    @ApiOperation("根据ID更新")
    @Log("根据ID更新")
    @SaCheckPermission("system:sysUser:update")
    @PostMapping("/updateById")
    public Result updateById(@RequestBody SysUserVo entity) {
        return sysUserService.updateUser(entity);
    }

    @ApiOperation("根据ID删除")
    @Log("根据ID删除")
    @SaCheckPermission("system:sysUser:remove")
    @GetMapping("/removeById/{id}")
    public Result removeById(@PathVariable("id") Long id) {
        return removeResult(sysUserService.removeById(id));
    }

    @ApiOperation("根据ID批量删除")
    @Log("根据ID批量删除")
    @SaCheckPermission("system:sysUser:removeBatch")
    @GetMapping("/removeBatchByIds")
    public Result removeBatchByIds(@PathVariable("ids") List<Long> ids) {
        return removeResult(sysUserService.removeBatchByIds(ids, ids.size()));
    }
}
